package com.chahu.servlet.backend;

import com.alibaba.fastjson.JSON;
import com.chahu.common.Result;
import com.chahu.entity.Order;
import com.chahu.service.SearchService;
import com.chahu.service.impl.SearchServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/static/backend/order/search")
public class SearchOrderServlet extends HttpServlet {
    SearchService searchService;

    @Override
    public void init() throws ServletException {
        searchService = new SearchServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json; charset=utf-8");
        String content = req.getParameter("content");

        List<Order> list = searchService.searchOrderByContent(content);
        try (PrintWriter writer = resp.getWriter()) {
            if(list == null){
                writer.write(JSON.toJSONString(Result.error(JSON.toJSONString("操作失败"))));
            }
            writer.write(JSON.toJSONString(Result.success(list)));
        }
    }
}
